package condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

public class ClassCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        Map<String ,Object> annotationAttributes=annotatedTypeMetadata.getAnnotationAttributes(ConditionalOnClass.class.getName());//获取注解属性
        Object value=annotationAttributes.get("className");
        if(value!=null){
            String className=value.toString();
            try {
                conditionContext.getClassLoader().loadClass(className);
            } catch (ClassNotFoundException e) {
                return false;
            }
        }

     return true;
    }
}
